####### make polar histograms for paper
###### el obeid and kampala

## file started by NN on 22 Feb 2022

#conda activate ox

import json
import multiprocessing as mp
import networkx as nx
import numpy as np
import os
import osmnx as ox
import pandas as pd
import random
import time
from scipy import stats

citygraphml = "al_ubayyid-4140.graphml"
#citygraphml = "kampala-4427.graphml"
#citygraphml = "accra-1910.graphml"
#citygraphml = "khartoum-4335.graphml"
#citygraphml = "nairobi-4808.graphml"

#country = "uganda-UGA_graphml"
#country = "ghana-GHA_graphml"
country = "sudan-SDN_graphml"
#country = "kenya-KEN_graphml"

#### PART 1: Frontmatter and define the functions

with open('config.json') as f: #NOTE TO REPLICATOR: you will need to set your own file paths for this to work in your own .json file
    config = json.load(f)

ox.config(log_file=True,
          logs_folder=config['osmnx_log_path'])

graphml_folder = config['models_graphml_path'] #where to load graphml files
indicators_street_path = config['indicators_street_path'] #where to save output street network indicators
save_every_n = 1 #save results every n cities

#### PART 2: try to make the polar histogram

filepath = os.path.join(graphml_folder, country, citygraphml)
G1 = ox.load_graphml(filepath=filepath)
G2 = G1.to_undirected()
ox.add_edge_bearings(G2, precision=1)

import matplotlib.pyplot as plt
#ox.bearing.plot_orientation(G2, num_bins=36, min_length=0,title="Khartoum", color="#99d8c9")
ox.bearing.plot_orientation(G2, num_bins=36, min_length=0,title="El Obeid", color="#99d8c9")
plt.savefig("polar_elobeid.jpg")


#citygraphml = "al_ubayyid-4140.graphml"
citygraphml = "kampala-4427.graphml"
#citygraphml = "accra-1910.graphml"
#citygraphml = "khartoum-4335.graphml"
#citygraphml = "nairobi-4808.graphml"

country = "uganda-UGA_graphml"
#country = "ghana-GHA_graphml"
#country = "sudan-SDN_graphml"
#country = "kenya-KEN_graphml"

#### PART 1: Frontmatter and define the functions

with open('config.json') as f: #NOTE TO REPLICATOR: you will need to set your own file paths for this to work in your own .json file
    config = json.load(f)

ox.config(log_file=True,
          logs_folder=config['osmnx_log_path'])

graphml_folder = config['models_graphml_path'] #where to load graphml files
indicators_street_path = config['indicators_street_path'] #where to save output street network indicators
save_every_n = 1 #save results every n cities

#### PART 2: try to make the polar histogram

filepath = os.path.join(graphml_folder, country, citygraphml)
G1 = ox.load_graphml(filepath=filepath)
G2 = G1.to_undirected()
ox.add_edge_bearings(G2, precision=1)

import matplotlib.pyplot as plt
#ox.bearing.plot_orientation(G2, num_bins=36, min_length=0,title="Khartoum", color="#99d8c9")
ox.bearing.plot_orientation(G2, num_bins=36, min_length=0,title="Kampala", color="#99d8c9")
plt.savefig("polar_kampala.jpg")
